package unimelb.distSystems.project2;

/*
 * Lim Ching Lih, 308721
 * Yu Xia, 351025
 * Carlos Vasconcelos, 193554
 */

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

/*
 * Custom panel for displaying various picture
 */
public class ImagePanel extends JPanel{
	private BufferedImage background_image=null;

	/*
	 * Constructor
	 */
    public ImagePanel(String filename){
        super();
        load_new_image(filename);
    }
    
    /*
     * Read picture from filesystem and store in memory
     * Input: String filename of picture file
     * Output: Nothing
     */
    public void load_new_image(String filename)
    {
    	if(filename==null)
    		return;
    	
    	try {
			background_image = ImageIO.read(new File(filename));
		} catch (IOException e) {
			System.out.println("ERROR: '"+filename+"' is not readable!");
		}
    }
    
    /*
     * Store the reference of BufferedImage object in memory
     * Input: Reference of BufferedImage object or empty
     * Output: Nothing
     */
    public void load_new_image(BufferedImage picture)
    {
    	background_image=picture;
    }

    /*
     * Paint the image within the panel if there is existing image
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
     */
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if(background_image!=null)
        	g.drawImage(background_image, 0, 0, null);
        
        validate();
		repaint();
    }
    
    /*
     * Return the width of stored image
     * Input: Nothing
     * Output: int width of image
     */
    public int getImageWidth()
    {
    	if(background_image!=null)
    		return background_image.getWidth();
    	else
    		return 0;
    }
    
    /*
     * Return the height of stored image
     * Input: Nothing
     * Output: int height of image
     */
    public int getImageHeight()
    {
    	if(background_image!=null)
    		return background_image.getHeight();
    	else
    		return 0;
    }
}
